unit edc;

interface

const
  EDC_crctable: Array [Byte] of LongWord = ($00000000, $90910101, $91210201,
    $01B00300, $92410401, $02D00500, $03600600, $93F10701, $94810801, $04100900,
    $05A00A00, $95310B01, $06C00C00, $96510D01, $97E10E01, $07700F00, $99011001,
    $09901100, $08201200, $98B11301, $0B401400, $9BD11501, $9A611601, $0AF01700,
    $0D801800, $9D111901, $9CA11A01, $0C301B00, $9FC11C01, $0F501D00, $0EE01E00,
    $9E711F01, $82012001, $12902100, $13202200, $83B12301, $10402400, $80D12501,
    $81612601, $11F02700, $16802800, $86112901, $87A12A01, $17302B00, $84C12C01,
    $14502D00, $15E02E00, $85712F01, $1B003000, $8B913101, $8A213201, $1AB03300,
    $89413401, $19D03500, $18603600, $88F13701, $8F813801, $1F103900, $1EA03A00,
    $8E313B01, $1DC03C00, $8D513D01, $8CE13E01, $1C703F00, $B4014001, $24904100,
    $25204200, $B5B14301, $26404400, $B6D14501, $B7614601, $27F04700, $20804800,
    $B0114901, $B1A14A01, $21304B00, $B2C14C01, $22504D00, $23E04E00, $B3714F01,
    $2D005000, $BD915101, $BC215201, $2CB05300, $BF415401, $2FD05500, $2E605600,
    $BEF15701, $B9815801, $29105900, $28A05A00, $B8315B01, $2BC05C00, $BB515D01,
    $BAE15E01, $2A705F00, $36006000, $A6916101, $A7216201, $37B06300, $A4416401,
    $34D06500, $35606600, $A5F16701, $A2816801, $32106900, $33A06A00, $A3316B01,
    $30C06C00, $A0516D01, $A1E16E01, $31706F00, $AF017001, $3F907100, $3E207200,
    $AEB17301, $3D407400, $ADD17501, $AC617601, $3CF07700, $3B807800, $AB117901,
    $AAA17A01, $3A307B00, $A9C17C01, $39507D00, $38E07E00, $A8717F01, $D8018001,
    $48908100, $49208200, $D9B18301, $4A408400, $DAD18501, $DB618601, $4BF08700,
    $4C808800, $DC118901, $DDA18A01, $4D308B00, $DEC18C01, $4E508D00, $4FE08E00,
    $DF718F01, $41009000, $D1919101, $D0219201, $40B09300, $D3419401, $43D09500,
    $42609600, $D2F19701, $D5819801, $45109900, $44A09A00, $D4319B01, $47C09C00,
    $D7519D01, $D6E19E01, $46709F00, $5A00A000, $CA91A101, $CB21A201, $5BB0A300,
    $C841A401, $58D0A500, $5960A600, $C9F1A701, $CE81A801, $5E10A900, $5FA0AA00,
    $CF31AB01, $5CC0AC00, $CC51AD01, $CDE1AE01, $5D70AF00, $C301B001, $5390B100,
    $5220B200, $C2B1B301, $5140B400, $C1D1B501, $C061B601, $50F0B700, $5780B800,
    $C711B901, $C6A1BA01, $5630BB00, $C5C1BC01, $5550BD00, $54E0BE00, $C471BF01,
    $6C00C000, $FC91C101, $FD21C201, $6DB0C300, $FE41C401, $6ED0C500, $6F60C600,
    $FFF1C701, $F881C801, $6810C900, $69A0CA00, $F931CB01, $6AC0CC00, $FA51CD01,
    $FBE1CE01, $6B70CF00, $F501D001, $6590D100, $6420D200, $F4B1D301, $6740D400,
    $F7D1D501, $F661D601, $66F0D700, $6180D800, $F111D901, $F0A1DA01, $6030DB00,
    $F3C1DC01, $6350DD00, $62E0DE00, $F271DF01, $EE01E001, $7E90E100, $7F20E200,
    $EFB1E301, $7C40E400, $ECD1E501, $ED61E601, $7DF0E700, $7A80E800, $EA11E901,
    $EBA1EA01, $7B30EB00, $E8C1EC01, $7850ED00, $79E0EE00, $E971EF01, $7700F000,
    $E791F101, $E621F201, $76B0F300, $E541F401, $75D0F500, $7460F600, $E4F1F701,
    $E381F801, $7310F900, $72A0FA00, $E231FB01, $71C0FC00, $E151FD01, $E0E1FE01,
    $7070FF00);

Function build_edc(Data: Pointer; Size: Integer): LongWord;

implementation

Function build_edc(Data: Pointer; Size: Integer): LongWord;
var
  P: PByte;
begin
  P := Data;
  Result := 0;
  Size := Size div 4;
  while (Size > 0) do
  begin
    Dec(Size);
    // result =  EDC_crctable[(result ^ *p++) & 0xffL] ^ (result >> 8);
    If Result = $2D5E3B78 Then
      ReadLn;
    Result := EDC_crctable[(Result xor P^) and $FF] xor (Result SHR 8);
    Inc(P);
    Result := EDC_crctable[(Result xor P^) and $FF] xor (Result SHR 8);
    Inc(P);
    Result := EDC_crctable[(Result xor P^) and $FF] xor (Result SHR 8);
    Inc(P);
    Result := EDC_crctable[(Result xor P^) and $FF] xor (Result SHR 8);
    Inc(P);
  end;
end;

end.
